package com.zixin.main;

import com.zixin.util.SM4Utils;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * main方法入口, 开启界面, 进行生成key与加密密码
 *
 * @author Jindou
 * @date 2021/11/2
 */
public class Sm4GenKeyWin {

    private JFrame frmSm;
    //如果未指定密钥, 则自动生成
    String s = "\u5982\u679c\u672a\u6307\u5b9a\u5bc6\u94a5\u002c\u0020\u5219\u81ea\u52a8\u751f\u6210";
    //明文密码不能为空
    String q = "\u660e\u6587\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a";
    //明文密码长度不能超过53个字符
    String w = "\u660e\u6587\u5bc6\u7801\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc7\u0035\u0033\u4e2a\u5b57\u7b26";
    //明文密码不能包含中文字符
    String e1 = "\u660e\u6587\u5bc6\u7801\u4e0d\u80fd\u5305\u542b\u4e2d\u6587\u5b57\u7b26";
    //key不能包含中文字符
    String r = "\u006b\u0065\u0079\u4e0d\u80fd\u5305\u542b\u4e2d\u6587\u5b57\u7b26";
    //key值错误!
    String t = "\u006b\u0065\u0079\u503c\u9519\u8bef\u0021";
    //密文解密
    String y1 = "\u5bc6\u6587\u89e3\u5bc6";
    //key不能为空
    String u = "\u006b\u0065\u0079\u4e0d\u80fd\u4e3a\u7a7a";
    //密文密码不能为空
    String i = "\u5bc6\u6587\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a";
    //密文密码不能包含中文字符
    String o = "\u5bc6\u6587\u5bc6\u7801\u4e0d\u80fd\u5305\u542b\u4e2d\u6587\u5b57\u7b26";
    //复制结果
    String p = "\u590d\u5236\u7ed3\u679c";
    //清空内容
    String a1 = "\u6e05\u7a7a\u5185\u5bb9";
    //SM4国密工具 v1.0
    String d = "\u0053\u004d\u0034\u56fd\u5bc6\u5de5\u5177\u0020\u0076\u0031\u002e\u0030";
    //明文密码：
    String f = "\u660e\u6587\u5bc6\u7801\uff1a";
    //密文密码：
    String g = "\u5bc6\u6587\u5bc6\u7801\uff1a";
    //明文加密
    String h = "\u660e\u6587\u52a0\u5bc6";
    //加密方式：国密SM4
    String j = "\u52a0\u5bc6\u65b9\u5f0f\uff1a\u56fd\u5bc6\u0053\u004d\u0034";
    //秘钥：
    String k = "\u79d8\u94a5\uff1a";

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Sm4GenKeyWin window = new Sm4GenKeyWin();
                    window.frmSm.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Sm4GenKeyWin() {
        initialize();
    }

    public boolean isEmpty(String text) {
        if (text == null || "".equals(text)) {
            return true;
        }
        return false;
    }

    public boolean isMoreThan53(String text) {
        return text.length() > 53 ? true : false;
    }


    public static boolean isContainChinese(String text) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(text);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        int width = 667;
        int height = 443;

        frmSm = new JFrame();
        frmSm.setResizable(false);
//        frmSm.setIconImage(Toolkit.getDefaultToolkit().getImage("..\\Sm4GenKeyWin\\image\\logo.png"));
//        frmSm.setTitle("SM4\u56FD\u5BC6\u5DE5\u5177");
        frmSm.setTitle(d);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double widthFull = screenSize.getWidth();
        double heightFull = screenSize.getHeight();

        // 屏幕的物理大小还需要知道屏幕的dpi 意思是说一英寸多少个象素
        double dpi = Toolkit.getDefaultToolkit().getScreenResolution();
        double a = widthFull / dpi;
        double b = heightFull / dpi;
//        System.out.println("屏幕尺寸是：" + Math.pow(a * a + b * b, 2));

        int x = (int) ((widthFull - width) / 2);
        int y = (int) ((heightFull - height) / 2);

        frmSm.setBounds(x, y, 667, 643);
        frmSm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmSm.getContentPane().setLayout(null);

        JLabel contentText = new JLabel(f);
        contentText.setFont(new Font("宋体", Font.BOLD, 23));
        contentText.setBounds(10, 10, 192, 36);
        frmSm.getContentPane().add(contentText);

        JLabel contentErrorText = new JLabel("");
        contentErrorText.setFont(new Font("宋体", Font.BOLD, 16));
        contentErrorText.setBounds(130, 10, 300, 36);
        contentErrorText.setForeground(Color.RED);
        frmSm.getContentPane().add(contentErrorText);


        JTextArea textBox_1 = new JTextArea();
        textBox_1.setBounds(10, 49, 630, 86);
        frmSm.getContentPane().add(textBox_1);


        JLabel resultText = new JLabel(g);
        resultText.setFont(new Font("宋体", Font.BOLD, 23));
        resultText.setBounds(10, 145, 192, 43);
        frmSm.getContentPane().add(resultText);


        JLabel resultErrorText = new JLabel("");
        resultErrorText.setFont(new Font("宋体", Font.BOLD, 16));
        resultErrorText.setBounds(130, 145, 192, 43);
        resultErrorText.setForeground(new Color(255, 0, 0));
        frmSm.getContentPane().add(resultErrorText);

        JTextArea textBox_2 = new JTextArea();
        textBox_2.setLineWrap(true);
        textBox_2.setBounds(10, 185, 630, 100);
        frmSm.getContentPane().add(textBox_2);


        JLabel keyText = new JLabel(k);
        keyText.setFont(new Font("宋体", Font.BOLD, 23));
        keyText.setBounds(10, 300, 192, 43);
        frmSm.getContentPane().add(keyText);


        JLabel keyErrorText = new JLabel(s);
        keyErrorText.setForeground(Color.RED);
        keyErrorText.setFont(new Font("宋体", Font.BOLD, 16));
        keyErrorText.setBounds(80, 300, 332, 43);
        frmSm.getContentPane().add(keyErrorText);


        JTextArea textBox_3 = new JTextArea();
        textBox_3.setLineWrap(true);
        textBox_3.setBounds(10, 340, 630, 100);
        frmSm.getContentPane().add(textBox_3);


        JLabel errText = new JLabel("");
        errText.setFont(new Font("宋体", Font.BOLD, 18));
        errText.setBounds(10, 500, 630, 100);
        frmSm.getContentPane().add(errText);


        JButton publicPsw = new JButton(h);
        publicPsw.addActionListener(new ActionListener() {
            /**
             * 获取加密密文
             */
            public void actionPerformed(ActionEvent e) {
                //初始化数据
                textBox_2.setText("");
                contentErrorText.setText("");
                resultErrorText.setText("");
                keyErrorText.setText(s);
                errText.setText("");

                String text = textBox_1.getText();
                if (isEmpty(text)) {
                    contentErrorText.setText(q);
                    return;
                }
                if (isMoreThan53(text)) {
                    contentErrorText.setText(w);
                    return;
                }
                if (isContainChinese(text)) {
                    contentErrorText.setText(e1);
                    return;
                }

                String keyTxt = textBox_3.getText();
                if (isContainChinese(keyTxt)) {
                    keyErrorText.setText(r);
                    return;
                }
                String encrypt = null;
                try {
                    keyTxt = SM4Utils.isGenKey(keyTxt);
                    textBox_3.setText(keyTxt);
                    encrypt = SM4Utils.encryptByEcb(text, keyTxt);
                } catch (Exception ex) {
                    if ("SM4 requires a 128 bit key".equals(ex.getMessage())) {
                        keyErrorText.setText(t);
                        keyErrorText.setForeground(new Color(255, 0, 0));
                    }
                    errText.setText(ex.getMessage());
                    ex.printStackTrace();
                }
                textBox_2.setText(encrypt);
            }
        });
        publicPsw.setFont(new Font("微软雅黑", Font.BOLD, 25));
        publicPsw.setBounds(10, 450, 148, 43);
        frmSm.getContentPane().add(publicPsw);

        //密文解密
        JButton privatePsw = new JButton(y1);
        privatePsw.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //初始化数据
                textBox_1.setText("");
                contentErrorText.setText("");
                resultErrorText.setText("");
                //如果未指定密钥, 则自动生成
                keyErrorText.setText(s);
                errText.setText("");

                String keyTxt = textBox_3.getText();
                if (isEmpty(keyTxt)) {
                    //key不能为空
                    keyErrorText.setText(u);
                    return;
                }
                if (isContainChinese(keyTxt)) {
                    //key不能包含中文字符
                    keyErrorText.setText(r);
                    return;
                }

                //校验字段
                String text = textBox_2.getText();
                if (isEmpty(text)) {
                    //密文密码不能为空
                    resultErrorText.setText(i);
                    return;
                }
                if (isContainChinese(text)) {
                    //密文密码不能包含中文字符
                    resultErrorText.setText(o);
                    return;
                }


                // 解密
                try {
                    textBox_1.setText(SM4Utils.decryptEcb(keyTxt, text));
                } catch (Exception ex) {
                    if ("SM4 requires a 128 bit key".equals(ex.getMessage())) {
                        //key值错误!
                        keyErrorText.setText(t);
                        keyErrorText.setForeground(new Color(255, 0, 0));
                    }
                    errText.setText(ex.getMessage());
                    ex.printStackTrace();
                }

            }
        });
        privatePsw.setFont(new Font("微软雅黑", Font.BOLD, 25));
        privatePsw.setBounds(168, 450, 145, 43);
        frmSm.getContentPane().add(privatePsw);

        //复制结果
        JButton copyResult = new JButton(p);
        copyResult.addActionListener(new ActionListener() {
            //复制结果
            public void actionPerformed(ActionEvent e) {
                String textBox1 = contentText.getText() + textBox_1.getText();
                String textBox2 = resultText.getText() + textBox_2.getText();
                String textBox3 = keyText.getText() + textBox_3.getText();
                //加密方式：国密SM4
                String writeMe = textBox1 + "\n" + textBox2 + "\n" + textBox3 + "\n" + j;
                Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
                Transferable tText = new StringSelection(writeMe);
                clip.setContents(tText, null);
            }
        });
        copyResult.setFont(new Font("微软雅黑", Font.BOLD, 25));
        copyResult.setBounds(323, 450, 152, 43);
        frmSm.getContentPane().add(copyResult);

        //清空内容
        JButton clearAll = new JButton(a1);
        clearAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textBox_1.setText("");
                textBox_2.setText("");
                textBox_3.setText("");
                contentErrorText.setText("");
                resultErrorText.setText("");
                keyErrorText.setText(s);
                errText.setText("");
            }
        });
        clearAll.setFont(new Font("微软雅黑", Font.BOLD, 25));
        clearAll.setBounds(485, 450, 145, 43);
        frmSm.getContentPane().add(clearAll);

    }
}


